from django.db import models


class NovelCategory(models.Model):
    """小说分类"""
    name = models.CharField(verbose_name="分类名称", max_length=100)
    url = models.CharField(verbose_name="分类连接", max_length=200, unique=True)
    sequence = models.FloatField(verbose_name="排序", default=0)

    class Meta:
        verbose_name = '小说分类'
        verbose_name_plural = verbose_name
        ordering = ['sequence']

    def __str__(self):
        return self.name

class Novel(models.Model):
    """小说"""
    name = models.CharField(verbose_name="小说名称", max_length=100, null=True, blank=True, default="")
    author = models.CharField(verbose_name="作者", max_length=100, null=True, blank=True, default="")
    url = models.CharField(verbose_name="小说连接", max_length=200, null=True, blank=True, default="", unique=True)
    img_url = models.CharField(verbose_name="小说封面", max_length=200, null=True, blank=True, default="")
    publish_date = models.CharField(verbose_name="更新日期", max_length=200, null=True, blank=True, default="")
    publish_state = models.CharField(verbose_name="小说状态", max_length=200, null=True, blank=True, default="")
    desc = models.TextField(verbose_name="小说简介")
    sequence = models.FloatField(verbose_name="排序", default=0)
    update_chapter = models.CharField(verbose_name="更新章节名", max_length=200, null=True, blank=True, default="")
    update_chapter_url = models.CharField(verbose_name="更新章节URL", max_length=200, null=True, blank=True, default="")
    novel_category = models.ForeignKey(NovelCategory, null=True, blank=True, on_delete=models.CASCADE, verbose_name="小说分类")

    class Meta:
        verbose_name = '小说'
        verbose_name_plural = verbose_name
        ordering = ['sequence']
        unique_together = ('name', 'author',)

    def __str__(self):
        return self.name

    @property
    def lasted_chapter(self):
        chapter_objs = self.novelchapter_set.order_by('-sequence')
        if len(chapter_objs):
            return chapter_objs[0]
        else:
            return False


DOING_STATE = (
    (1, "草稿"),
    (2, "成功"),
    (3, "异常"),
)

class NovelChapter(models.Model):
    """小说章节"""
    name = models.CharField(verbose_name="章节名", max_length=100, null=True, blank=True, default="")
    url = models.CharField(verbose_name="章节连接", max_length=200, null=True, blank=True, default="", unique=True)
    content = models.TextField(verbose_name="章节内容")
    sequence = models.FloatField(verbose_name="排序", default=0)
    novel = models.ForeignKey(Novel, null=True, blank=True, on_delete=models.CASCADE, verbose_name="小说")
    state = models.IntegerField(choices=DOING_STATE, verbose_name="获取状态", default=1)

    class Meta:
        verbose_name = '小说章节'
        verbose_name_plural = verbose_name
        ordering = ['sequence']

    def __str__(self):
        return self.name

    @property
    def html_content(self):
        return "<br/>".join(self.content.split())

    @property
    def next_chapter(self):
        objs = NovelChapter.objects.filter(novel_id=self.novel_id, sequence=self.sequence+1)
        if len(objs):
            return objs[0]
        else:
            return False

    @property
    def prev_chapter(self):
        objs = NovelChapter.objects.filter(novel_id=self.novel_id, sequence=self.sequence-1)
        if len(objs):
            return objs[0]
        else:
            return False

'''
set character_set_client=gbk;
set character_set_results=gbk;
'''

